package com.yy.ssjx.controller;

import cn.hutool.core.lang.UUID;
import com.yy.ssjx.domain.ShardingXaTest1;
import com.yy.ssjx.domain.ShardingXaTest2;
import com.yy.ssjx.mapper.ShardingXaTest1Mapper;
import com.yy.ssjx.mapper.ShardingXaTest2Mapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述: sharding-jdbc-xa分布式事务对象
 *
 * @author: yanglin
 * @Date: 2021-11-08-14:10
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/api/tx")
public class ShardingXaTestController {

    @Autowired
    private ShardingXaTest1Mapper xaTest1Mapper;

    @Autowired
    private ShardingXaTest2Mapper xaTest2Mapper;

    /**
     * https://shardingsphere.apache.org/document/legacy/4.x/document/cn/features/transaction/
     * https://shardingsphere.apache.org/document/legacy/4.x/document/cn/manual/sharding-jdbc/usage/transaction/#%E9%85%8D%E7%BD%AEspring-boot%E7%9A%84%E4%BA%8B%E5%8A%A1%E7%AE%A1%E7%90%86%E5%99%A8
     *
     * XA事务管理器参数配置（可选）
     * ShardingSphere默认的XA事务管理器为Atomikos，在项目的logs目录中会生成xa_tx.log,
     * 这是XA崩溃恢复时所需的日志，请勿删除。
     * 也可以通过在项目的classpath中添加jta.properties来定制化Atomikos配置项。
     *
     * BASE柔性事务管理器（SEATA-AT配置）
     * 1.按照seata-work-shop中的步骤，下载并启动seata server，参考 Step6 和 Step7即可。
     * 2.在每一个分片数据库实例中执创建undo_log表（以MySQL为例）
     * 3.在classpath中增加seata.conf
     * 4.根据实际场景修改seata的file.conf和registry.conf文件
     */

    /**
     * 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
     * http://localhost:10013/api/xa/save
     * @return
     */
    @Transactional
    @ShardingTransactionType(TransactionType.XA)
    @GetMapping("/saveXa")
    public String insertXaTest(){
        // d_sharding_xa_test1表在(ds0)的lth库
        ShardingXaTest1 xaTest1 = new ShardingXaTest1();
        xaTest1.setXaName(UUID.randomUUID().toString()+"-xa");
        xaTest1.setXaRemark(UUID.randomUUID().toString()+"-xamark");
        xaTest1Mapper.insert(xaTest1);
        // d_sharding_xa_test2表在(ds1)的mumu库
        ShardingXaTest2 xaTest2 = new ShardingXaTest2();
        xaTest2.setXaName(UUID.randomUUID().toString()+"-xa");
        xaTest2.setXaRemark(UUID.randomUUID().toString()+"-xamark");
        xaTest2Mapper.insert(xaTest2);
        return "success";
    }

    @Transactional
    @ShardingTransactionType(TransactionType.BASE)
    @GetMapping("/saveBase")
    public String insertBaseTest(){
        // d_sharding_xa_test1表在(ds0)的lth库
        ShardingXaTest1 xaTest1 = new ShardingXaTest1();
        xaTest1.setXaName(UUID.randomUUID().toString()+"-base");
        xaTest1.setXaRemark(UUID.randomUUID().toString()+"-basemark");
        xaTest1Mapper.insert(xaTest1);
        // d_sharding_xa_test2表在(ds1)的mumu库
        ShardingXaTest2 xaTest2 = new ShardingXaTest2();
        xaTest2.setXaName(UUID.randomUUID().toString()+"-base");
        xaTest2.setXaRemark(UUID.randomUUID().toString()+"-basemark");
        xaTest2Mapper.insert(xaTest2);
        return "success";
    }

}
